﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SharpLite.Domain.DataInterfaces;
using DAEProde.Domain;
using DAEProde.Tasks;

namespace DAEProde.Web.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class MatchDateManagerController : Controller
    {
        private IRepository<MatchDate> _matchDateRepository;
        private IRepository<Tournament> _tournametRepository;
        private MatchDateCudTasks _matchDateMgmtTasks;

        public MatchDateManagerController(IRepository<MatchDate> matchDateRepository, IRepository<Tournament> tournametRepository, MatchDateCudTasks matchDateMgmtTasks)
        {
            _matchDateRepository = matchDateRepository;
            _tournametRepository = tournametRepository;
            _matchDateMgmtTasks = matchDateMgmtTasks;
        }

        //
        // GET: /MatchDateManager/

        public ActionResult Index(int id)
        {
            return View(_tournametRepository.Get(id));
        }

        public ActionResult Create(int id)
        {
            return View("Edit", _matchDateMgmtTasks.CreateViewModel(id));
        }

        public ActionResult Details(int id)
        {
            return View(_matchDateRepository.Get(id));
        }

        public ActionResult Edit(int id)
        {
            return View(_matchDateMgmtTasks.CreateEditViewModel(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(MatchDate matchDate)
        {
            if (ModelState.IsValid)
            {
                ActionConfirmation<MatchDate> confirmation = _matchDateMgmtTasks.SaveOrUpdate(matchDate);

                if (confirmation.WasSuccessful)
                {
                    TempData["message"] = confirmation.Message;
                    return RedirectToAction("Index", new { id = matchDate.Tournament.Id });
                }

                ViewData["message"] = confirmation.Message;
            }

            return View(_matchDateMgmtTasks.CreateEditViewModel(matchDate));
        }

        public ActionResult Delete(int id)
        {
            var tmpId = _matchDateRepository.Get(id).Tournament.Id;

            ActionConfirmation<MatchDate> confirmation = _matchDateMgmtTasks.Delete(id);
            TempData["message"] = confirmation.Message;

            return RedirectToAction("Index", new { id = tmpId });
        }

        public ActionResult LoadResult(int id)
        {
            return View(_matchDateRepository.Get(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LoadResult(FormCollection c)
        {
            var tId = c.GetValue("tId");
            var mId = c.Get(2);
            var aux = c.Get(3);
            var results = aux.Split(',');

            if (ModelState.IsValid)
            {
                ActionConfirmation<MatchDate> confirmation = _matchDateMgmtTasks.SaveResults(results, int.Parse(mId));

                if (confirmation.WasSuccessful)
                {
                    TempData["message"] = confirmation.Message;
                    return RedirectToAction("Index", new { id = int.Parse(tId.AttemptedValue) });
                }

                ViewData["message"] = confirmation.Message;
            }

            return View(_matchDateMgmtTasks.CreateEditViewModel(int.Parse(mId)));
        }
    }
}
